/**
 * FlightInfoDbJDOImpl.java
 * 2010-1-26
 */
package com.dlplay.flightinfo.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.persistence.EntityManager;

import com.dlplay.flightinfo.data.AirFlightCode;
import com.dlplay.flightinfo.data.Airport;
import com.dlplay.flightinfo.data.AirportTerminalCode;
import com.dlplay.flightinfo.data.CityCode;
import com.dlplay.flightinfo.data.EMailRegister;
import com.dlplay.flightinfo.data.FlightInfo;
import com.dlplay.flightinfo.data.RealTimeFlightInfo;
import com.dlplay.flightinfo.db.datastore.PMF;

/**
 * @author zhao yu
 *
 */
public class FlightInfoDbJDOImpl implements FlightInfoDbMngr {
	private static final Logger log = Logger.getLogger(FlightInfoDbJDOImpl.class.getName());		

	@Override
	public void addAirport(Airport airport) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addFlightRegister(EMailRegister reg) {
		log.info("add register: "+reg);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(reg);
		} finally {
			pm.close();
		}		
	}

	@Override
	public void addRealTimeFlightInfo(RealTimeFlightInfo info) {
		log.info("addRealTimeFlightInfo: "+info);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(info);
		} finally {
			pm.close();
		}
		log.info("add a new record");
	}

	@Override
	public void deleteAirport(AirportTerminalCode terminal) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Airport getAirport(AirportTerminalCode terminal) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Airport> getAirports() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EntityManager getEntityManager() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public EMailRegister getFlightEmailRegister(AirFlightCode flightNum,
			String email) {
		log.info("get EmailRegister: "+flightNum+"email: "+email);
		PersistenceManager pm = PMF.get().getPersistenceManager();		
		Query query = pm.newQuery(EMailRegister.class); 
		query.setFilter("number == "+flightNum.getNumber()+" && airline == '"+flightNum.getCode()+"' && email == em"); 		
//		query.setFilter("number == '"+flightNum.getNumber()+"' && airline == '"
//				+flightNum.getCode()+"' && email == em"); 
		query.declareParameters("String em"); 
		try { 
			List<EMailRegister> results = (List<EMailRegister>) query.execute(email); 
			if(results.size()>0)
			{
				EMailRegister f = results.get(0);
				log.info("find flight: "+f);
				return f;
			}			
		} finally { 
			query.closeAll(); 
		}
		return null;
	}

	@Override
	public List<FlightInfo> getFlightInfo(CityCode from, CityCode to, Date date) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FlightInfo getFlightInfo(AirFlightCode flightNumber, Date date) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public RealTimeFlightInfo getRealTimeFlightInfo(AirFlightCode flightNumber) {
		log.info("getRealTimeFlightInfo: "+flightNumber);
		PersistenceManager pm = PMF.get().getPersistenceManager();		
		Query query = pm.newQuery(RealTimeFlightInfo.class); 
		query.setFilter("key == lastNameParam"); 
		query.declareParameters("String lastNameParam"); 		 
		try { 
			List<RealTimeFlightInfo> results = (List<RealTimeFlightInfo>) query.execute(flightNumber.toString()); 
			if(results.size()>0)
			{
				RealTimeFlightInfo f = results.get(0);
				log.info("find flight: "+f);
				return f;
			}			
		} finally { 
			query.closeAll(); 
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AirFlightCode> getRegisteredFlights() {
		log.info("get Register flights");
		List<AirFlightCode> flights = new ArrayList<AirFlightCode>();		
		PersistenceManager pm = PMF.get().getPersistenceManager();		
		Query query = pm.newQuery(EMailRegister.class); 		 		
		try { 
			//Fetch all
			List<EMailRegister> results = (List<EMailRegister>) query.execute(); 
			for(EMailRegister r: results)
			{
				AirFlightCode flightNum = new AirFlightCode(r.getAirline(), r.getNumber());
				if(!flights.contains(flightNum))
				{
					flights.add(flightNum);
				}
			}		
		} finally { 
			query.closeAll(); 
		}
		return flights;
	}

	@Override
	public void updateAirport(Airport airport) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateRealTimeFlightInfo(RealTimeFlightInfo info) {
		log.info("update flight: "+info);
	    PersistenceManager pm = PMF.get().getPersistenceManager(); 
	    try { 
	    	RealTimeFlightInfo e = pm.getObjectById(RealTimeFlightInfo.class, info.getKey()); 
	        e.setLatestUpdate(info.getLatestUpdate());
	        e.setActualArrival(new Date());
	    } finally { 
	        pm.close(); 
	    } 		
	}

}
